﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Data.Entity;
using EventPlanner.Models;

namespace EventPlanner.tests.Dependencias
{
    class FakeDBSets
    {
        public class FakeDbSet<T> : IDbSet<T>
    where T : class
        {
            ObservableCollection<T> _data;
            IQueryable _query;

            public FakeDbSet()
            {
                _data = new ObservableCollection<T>();
                _query = _data.AsQueryable();
            }

            public virtual T Find(params object[] keyValues)
            {
                throw new NotImplementedException("Derive from FakeDbSet<T> and override Find");
            }

            public T Add(T item)
            {
                _data.Add(item);
                return item;
            }

            public T Remove(T item)
            {
                _data.Remove(item);
                return item;
            }

            public T Attach(T item)
            {
                _data.Add(item);
                return item;
            }

            public T Detach(T item)
            {
                _data.Remove(item);
                return item;
            }

            public T Create()
            {
                return Activator.CreateInstance<T>();
            }

            public TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity : class, T
            {
                return Activator.CreateInstance<TDerivedEntity>();
            }

            public ObservableCollection<T> Local
            {
                get { return _data; }
            }

            Type IQueryable.ElementType
            {
                get { return _query.ElementType; }
            }

            System.Linq.Expressions.Expression IQueryable.Expression
            {
                get { return _query.Expression; }
            }

            IQueryProvider IQueryable.Provider
            {
                get { return _query.Provider; }
            }

            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return _data.GetEnumerator();
            }

            IEnumerator<T> IEnumerable<T>.GetEnumerator()
            {
                return _data.GetEnumerator();
            }
        }

        public class FakeCuentaSet : FakeDbSet<Cuenta>
        {
            public override Cuenta Find(params object[] keyValues)
            {
                return this.SingleOrDefault(b => b.NombreUsuario == (string)keyValues.Single());
            }
        }
        public class FakeContactoSet : FakeDbSet<Contacto>
        {
            public override Contacto Find(params object[] keyValues)
            {
                return this.SingleOrDefault(d => d.Email == (string)keyValues.Single());
            }
        }
        public class FakeParticipanteSet : FakeDbSet<Participante>
        {
            public override Participante Find(params object[] keyValues)
            {
                return this.SingleOrDefault(s => s.Email == (string)keyValues.Single());
            }
        }
        public class FakeEventoSet : FakeDbSet<Evento>
        {
            public override Evento Find(params object[] keyValues)
            {
                return this.SingleOrDefault(e => e.EventoID == (int)keyValues.Single());
            }
        }
        public class FakeOrganizadorSet : FakeDbSet<Organizador>
        {
            public override Organizador Find(params object[] keyValues)
            {
                return this.SingleOrDefault(f => f.OrganizadorID == (int)keyValues.Single());
            }
        }

    }
}